﻿using UnityEngine;

public class ElementAura : IElementAura
{
    private float _gaugeLossSpeed;
    private float _gauge;

    public GameObject Source { get; set; }

    /// <summary>
    /// 元素类型
    /// </summary>
    public ElementType ElementType { get; }

    public float Gauge
    {
        get => _gauge;
        set => _gauge = value <= 0f ? 0f : value;
    }

    public bool IsGaugeLoss { get; set; }

    public ElementAura(ElementType elementType)
    {
        ElementType = elementType;
    }

    public virtual void Attach(float gauge, GameObject source)
    {
        // Debug.Log($"附着元素量：{ElementType} | {gauge}");
        
        var hasGauge = Gauge > 0f;
        
        // 附着元素量损耗20%
        Gauge = Mathf.Max(Gauge, gauge * 0.8f);
        
        // 继承衰退速度
        if (!hasGauge)
        {
            _gaugeLossSpeed = Gauge / (7f + gauge * 2.5f);
        }
  
        Source = source;
    }

    public virtual void Update(float deltaTime)
    {
        if (Gauge > 0f)
        {
            Gauge -= _gaugeLossSpeed * deltaTime;
        }
    }
}